﻿using System;
using System.Collections.Generic;
using System.Linq;
using FineUI;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.OrganizationalLife
{
    public partial class MemberCostTotalList : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyMemberCostView";
            }
        }


        public string Category
        {
            get
            {
                return Request.QueryString["Category"] ?? "BranchHonour";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        #region 数据加载
        private void BindData()
        {
            var year = DateTime.Now.Year;
            for (int i = year - 5; i <= year + 5; i++)
            {
                this.ddlYear.Items.Add(new ListItem() { Text = i.ToString(), Value = i.ToString() });
            }
            this.ddlYear.SelectedValue = year.ToString();

            // 每页记录数
            GridTotal.PageSize = ConfigHelper.PageSize;
            ddlGridPageSize.SelectedValue = ConfigHelper.PageSize.ToString();
            // 权限检查
            CheckPowerWithButton("PartyMemberCostAdd", btnNew);
            btnNew.OnClientClick = Window1.GetShowReference("PartyMemberCostEdit.aspx?Category=" + Category, "新增");
            this.ppBranch.WindinsUrl = "../../../Share/Picker/BranchSinglePicker.aspx?BranchID=" + BranchID;
            this.ppBranch.Value = BranchID;
            this.ppBranch.Text = ControlBusiness.GetBranchPickerText(BranchID);
            this.ppBranch.ShowTrigger1 = false;
        }
        private void LoadData()
        {
            var branch = DB.PartyOrgBranchs.Find(this.ppBranch.Value);
            if ((new List<string>() { "3", "5" }).Contains(branch.BranchMark))
            {
                //跳到缴纳页面
                Response.Redirect("MemberCostList.aspx?BranchID=" + this.ppBranch.Value);
            }

            var ds = PartyMemberCost.GetBranchCostTotal(GridTotal.PageIndex, GridTotal.PageSize, this.ppBranch.Value,
                Convert.ToInt32(ddlYear.SelectedValue),
               txtTitle.Text);

            GridTotal.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            GridTotal.DataSource = ds.Tables[0];
            GridTotal.DataBind();
        }



        protected void GridTotal_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithWindowField("PartyMemberCostEdit", GridTotal, "editField");
            CheckPowerWithLinkButtonField("PartyMemberCostDelete", GridTotal, "deleteField");
        }

        protected void GridTotal_Sort(object sender, GridSortEventArgs e)
        {
            GridTotal.SortDirection = e.SortDirection;
            GridTotal.SortField = e.SortField;
            LoadData();
        }

        protected void GridTotal_PageIndexChange(object sender, GridPageEventArgs e)
        {
            GridTotal.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void GridTotal_RowCommand(object sender, GridCommandEventArgs e)
        {
            string PartyMemberCostId = GetSelectedDataKeyID(GridTotal);

            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!CheckPower("PartyMemberCostDelete"))
                {
                    CheckPowerFailWithAlert();
                    return;
                }

                var userCount = DB.PartyMemberCosts.Count(t => t.ID == PartyMemberCostId);
                if (userCount > 0)
                {
                    Alert.ShowInTop("删除失败！需要先清空作用此专题的题目！");
                    return;
                }

                var del = DB.PartyMemberCosts.FirstOrDefault(t => t.ID == PartyMemberCostId);
                DB.PartyMemberCosts.Remove(del);
                DB.SaveChanges();
                LoadData();
            }
        }
        #endregion

        #region 按钮事件
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }
        protected void btnDeleteSelected_Click(object sender, EventArgs e)
        {
            // 在操作之前进行权限检查
            if (!CheckPower("PartyMemberCostDelete"))
            {
                CheckPowerFailWithAlert();
                return;
            }

            // 从每个选中的行中获取ID（在GridTotal中定义的DataKeyNames）
            List<string> ids = GetSelectedDataKeyIDs(GridTotal);

            // 执行数据库操作
            DB.PartyMemberCosts.Where(u => ids.Contains(u.ID)).ToList().ForEach(u => DB.PartyMemberCosts.Remove(u));
            DB.SaveChanges();

            // 重新绑定表格
            LoadData();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridTotal.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            LoadData();
        }

        
        #endregion

        protected void Dialog_Close(object sender, EventArgs e)
        {
            LoadData();
        }


        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            LoadData();
        }


        
    }
}